package com.example.transctiondemo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @program: transction-demo
 * @description:
 * @author: kangwei
 * @create: 2024-06-03 11:03
 **/
@ComponentScan
@EnableTransactionManagement
public class DataSourceConfig {


    /**
     * 注入dataSource bean
     *
     * @return
     */
    @Bean
    public DataSource dataSource() {
        return new DruidDataSource();
    }


    /**
     * 注入jdbcTemplate bean
     * 需传递dataSource
     *
     * @return
     */
    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    /**
     * 注入事务管理器
     *
     * @return
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

}
